//
//  Fraction.m
//  OC03_属性_点语法
//
//  Created by dllo on 16/5/24.
//  Copyright © 2016年 Rain. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction

-(instancetype)initWithMember:(NSInteger)member withDenominator:(NSInteger)denominator{
    self = [super init];
    if(self){
        _member = member;
        _denominator = denominator;
    }
    return self;
}

+(instancetype)FractionWithMember:(NSInteger)member withDenominator:(NSInteger)denominator{
    Fraction *fr = [[Fraction alloc] initWithMember:member withDenominator:denominator];
    return fr;
}
#pragma mark - 加法
//加法
-(Fraction *)addWithFra:(Fraction *)fra{
    Fraction *fraSum =[[Fraction alloc]init];
    fraSum.member = self.member * fra.denominator + fra.member * self.denominator;
    fraSum.denominator = self.denominator * fra.denominator;
    [fraSum reduction];
    return fraSum;
}


+(Fraction *)addXWithFra1:(Fraction *)fra1 withFra2:(Fraction *)fra2{
    Fraction *fraSum = [[Fraction alloc] init];
    fraSum.member = fra1.member * fra2.denominator + fra1.denominator * fra2.member;
    fraSum.denominator = fra1.denominator *fra2.denominator;
    [fraSum reduction];
    return fraSum;
}
#pragma mark - 减法
//减法
-(Fraction *)minuWithFra:(Fraction *)fra{
    Fraction *fraMin =[[Fraction alloc] init];
    fraMin.member = self.member * fra.denominator - fra.member * self.denominator;
    fraMin.denominator = self.denominator * fra.denominator;
    [fraMin reduction];
    return fraMin;
}
#pragma mark - 乘法
//乘法
-(Fraction *)multipWithFra:(Fraction *)fra{
    Fraction *fraMul = [[Fraction alloc] init];
    fraMul.member = self.member * fra.member;
    fraMul.denominator = self.denominator * fra.denominator;
    [fraMul reduction];
    return fraMul;
}
#pragma mark - 除法
//除法
-(Fraction *)divisionWithFra:(Fraction *)fra{
    Fraction *fraDiv = [[Fraction alloc] init];
    
    Fraction *fraBottom = [[Fraction alloc] init];
    fraBottom.member = fra.denominator;
    fraBottom.denominator = fra.member;
    
    fraDiv = [self multipWithFra:fraBottom];
    
    [fraDiv reduction];
    return fraDiv;
}
#pragma mark - 约分
//约分
-(void)reduction{
    NSInteger maxGCD = [self maxGCDWith:self.member num2:self.denominator];
    self.member /= maxGCD;
    self.denominator /= maxGCD;
}
#pragma mark - 最大公约数
//求最大公约数
-(NSInteger)maxGCDWith:(NSInteger)num1 num2:(NSInteger)num2{
    int newNum1 = abs((int)num1);
    int newNum2 = abs((int)num2);
    
    while (newNum1 % newNum2){
        int temp = newNum1 % newNum2;
        newNum1 = newNum2;
        newNum2 = temp;
    }
    return newNum2;
    
}
#pragma mark - 比较
//比较
-(NSComparisonResult)compareWith:(Fraction *)otherFraction{
    Fraction *newFra = [self minuWithFra:otherFraction];
    if (newFra.member * newFra.denominator > 0){
        return NSOrderedDescending;//降序  即左边的分数比右边的大
    } else if (newFra.member * newFra.denominator < 0){
        return NSOrderedAscending;//升序  即左边的分数比右边小
    } else {
        return NSOrderedSame;
    }
}
    
    



@end
